/**
 * Copyright 2009 Fabian Henniges, Henning Teek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigscommons.web;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;

import brix.web.nodepage.PageParametersCarryingLink;

import com.googlecode.chigscommons.domain.User;
import com.googlecode.chigscommons.web.auth.LoginPage;

/**
 * @author Henning Teek
 */
public class UserPanel extends Panel {
	public UserPanel(String id) {
		super(id);
		
        boolean signedIn = ((WicketWebSession)getSession()).isSignedIn();
		User user = signedIn ? ((WicketWebSession)getSession()).getUser() : null;

		add(new Label("greeting", user != null ? new StringResourceModel("greeting", new Model<User>(user)) : new Model<String>("")));
		add(new PageParametersCarryingLink("settingsLink", ((WicketApplication)getApplication()).getI18nBrixLink(getSession(), "/settings.html")));
		add(new Link<Void>("logoutLink") {
			@Override
			public void onClick() {
				getSession().invalidate();
				getRequestCycle().setRequestTarget(new RedirectRequestTarget("/"));
			}
		}.setVisible(signedIn));
		
		add(new BookmarkablePageLink<LoginPage>("loginLink", LoginPage.class).setVisible(!signedIn));
		add(new PageParametersCarryingLink("signupLink", ((WicketApplication)getApplication()).getI18nBrixLink(getSession(), "/signup.html")));
	}
}
